package com.elitel.frame.base.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.elitel.frame.base.service.AcServiceFilterFieldService;
import com.elitel.frame.main.dao.ext.*;
import com.elitel.frame.main.entity.ext.PrjServiceExt;
import com.elitel.frame.main.entity.vo.SingleTableInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.CfgExtendService;
import com.elitel.frame.base.component.RedisCacheComponent;
import com.elitel.frame.base.component.RedisCacheProjectService;
import com.elitel.frame.main.dao.CfgDbMapper;
import com.elitel.frame.main.dao.CfgExtendMapper;
import com.elitel.frame.main.dao.SysPrjserviceMapper;
import com.elitel.frame.main.entity.CfgDb;
import com.elitel.frame.main.entity.CfgExtend;
import com.elitel.frame.main.entity.SysPrjservice;
import com.elitel.frame.main.entity.ext.CfgExtendExt;
import com.elitel.frame.main.entity.vo.PageRequestVo;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.elitel.frame.main.entity.vo.ZTreeDataModelVo;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author guoyanfei
 * @Description 单表配置服务实现类
 */
@Service
public class CfgExtendServiceImpl implements CfgExtendService {

    @Autowired
    private CfgExtendMapper cfgExtendMapper;
    @Autowired
    private ExtCfgExtendMapper extCfgExtendMapper;
    @Autowired
    private RedisCacheComponent redisCacheComponent;
    @Autowired
    private ExtCfgDbMapper extCfgDbMapper;
    @Autowired
    private ExtBaseQueryfieldMapper extBaseQueryfieldMapper;
    @Autowired
    private ExtBaseQueryfieldcacheMapper extBaseQueryfieldcacheMapper;
    @Autowired
    private SysPrjserviceMapper sysPrjserviceMapper;
    @Autowired
    private ExtSysPrjserviceMapper extSysPrjserviceMapper;

    @Autowired
    private AcServiceFilterFieldService acServiceFilterFieldService;

    @Autowired
    private ExtBaseShowfieldMapper extBaseShowfieldMapper;
    @Autowired
    private ExtBaseQuerysqlMapper extBaseQuerysqlMapper;
    @Autowired
    private CfgDbMapper cfgDbMapper;
    @Autowired
    private RedisCacheProjectService redisCacheProjectService;

    @Override
    public CfgExtend selectByPrimaryKey(String guid) {
        CfgExtend cfgExtend = cfgExtendMapper.selectByPrimaryKey(guid);
        PrjServiceExt prjServiceExt = sysPrjserviceMapper.selectByServiceCode(guid);
        cfgExtend.setResCvsJs(prjServiceExt.getResCvsJs());
        cfgExtend.setApiPath(prjServiceExt.getApiPath());
        cfgExtend.setCacheDur(prjServiceExt.getCacheDur());
        return cfgExtend;
    }

    @Override
    public PageResponseVo<CfgExtendExt> getListByPage(PageRequestVo<CfgExtend> pr) {
        pr.StartPage();
        List<CfgExtendExt> list = extCfgExtendMapper.getAllList(pr);
        Integer total = extCfgExtendMapper.getListCount(pr);
        PageResponseVo<CfgExtendExt> responseVo = new PageResponseVo<CfgExtendExt>();
        responseVo.setRows(list);
        responseVo.setTotal(total);

        return responseVo;
    }

    @Override
    public Integer deleteCfgExtendByguid(String serviceId) {
        Integer result = 0;
        //删除单表服务
        result += cfgExtendMapper.deleteByPrimaryKey(serviceId);
        //删除查询字段
        extBaseQueryfieldMapper.deleteQueryfieldByServiceId(serviceId);
        //删除查找字段条件sql
        extBaseQuerysqlMapper.deleteQuerysqlByServiceId(serviceId);
        //删除显示字段
        extBaseShowfieldMapper.deleteShowfieldByServiceId(serviceId);
        redisCacheComponent.freshExtendRedisCache();

        return result;
    }

    @Override
    public void deleteCfgExtendByPrjservice(SysPrjservice prjservice) {
        //删除查询条件缓存字段配置
        Integer size = extBaseQueryfieldcacheMapper.deleteQueryFieldCacheListByServiceId(prjservice.getServicecode());
        //删除查找字段条件sql
        extBaseQuerysqlMapper.deleteQuerysqlByServiceId(prjservice.getServicecode());
        //删除显示字段
        extBaseShowfieldMapper.deleteShowfieldByServiceId(prjservice.getServicecode());
        //删除查询条件字段
        extBaseQueryfieldMapper.deleteQueryfieldByServiceId(prjservice.getServicecode());
        //删除单表服务
        sysPrjserviceMapper.deleteByPrimaryKey(prjservice.getGuid());
        cfgExtendMapper.deleteByPrimaryKey(prjservice.getServicecode());
        redisCacheComponent.freshExtendRedisCache();
        redisCacheProjectService.freshProjectServerTree();

    }

    @Override
    public List<SingleTableInfoVo> selectByServiceCodes(List<String> serviceCodes) {
        return cfgExtendMapper.selectByServiceCodes(serviceCodes);
    }


    @Override
    public int insertData(CfgExtend cfgExtend) {
        int insertSelective = cfgExtendMapper.insertSelective(cfgExtend);
        redisCacheComponent.freshExtendRedisCache();
        return insertSelective;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateData(CfgExtend cfgExtend, CfgExtendExt cfgExtendExt) {
        //修改时检查表是否修改，如修改，则需要删除之前配置的显示字段和查询字段
        CfgExtend cfgExtendold = cfgExtendMapper.selectByPrimaryKey(cfgExtend.getGuid());
        if (!cfgExtend.getDtConfigId().equals(cfgExtendold.getDtConfigId())) {
            //删除显示列
            extBaseShowfieldMapper.deleteShowfieldByServiceId(cfgExtend.getGuid());
            //删除查询列
            extBaseQueryfieldMapper.deleteQueryfieldByServiceId(cfgExtend.getGuid());
            //删除查询sql文
            extBaseQuerysqlMapper.deleteQuerysqlByServiceId(cfgExtend.getGuid());
        }

        // 先只更新服务标签
        SysPrjservice sysPrjservice = new SysPrjservice();
        sysPrjservice.setServicecode(cfgExtend.getGuid());
        sysPrjservice.setSysDataLabelId(cfgExtendExt.getSysDataLabelId());
        sysPrjservice.setCacheDur(cfgExtendExt.getCacheDur());
        sysPrjservice.setApiPath(cfgExtendExt.getApiPath());
        sysPrjservice.setResCvsJs(cfgExtendExt.getResCvsJs());
        sysPrjservice.setServicename(cfgExtendExt.getServicename());
        extSysPrjserviceMapper.updateServer(sysPrjservice);

        int updateByPrimaryKeySelective = cfgExtendMapper.updateByPrimaryKeySelective(cfgExtend);
        // 新增过滤字段
        acServiceFilterFieldService.insert(cfgExtendExt.getFieldFormVo());
        redisCacheComponent.freshExtendRedisCache();
        return updateByPrimaryKeySelective;
    }

    @Override
    public boolean existByguid(String guid) {
        boolean result = false;
        CfgExtend cfgExtend = cfgExtendMapper.selectByPrimaryKey(guid);
        if (cfgExtend != null) {
            result = true;
        }
        return result;
    }

    @Override
    public List<CfgExtend> selectAllExtend() {
        List<CfgExtend> list = extCfgExtendMapper.selectAllExtend();
        return list;
    }

    @Override
    public List<ZTreeDataModelVo> getTreeCfgExtendList() {
        List<ZTreeDataModelVo> ztreeList = new ArrayList<ZTreeDataModelVo>();
        List<CfgDb> cfgdbList = extCfgDbMapper.getAllListCfgDb();
        if (cfgdbList != null && cfgdbList.isEmpty() == false) {
            for (CfgDb cb : cfgdbList) {
                ZTreeDataModelVo ztmodelDataModelVo = new ZTreeDataModelVo();
                ztmodelDataModelVo.setId(cb.getDbKey());
                ztmodelDataModelVo.setName(cb.getDbMark() + "(" + cb.getDbName() + ")");
                ztmodelDataModelVo.setHint("数据库版本:" + cb.getDbVersion() + ",数据库名称:" + cb.getDbName());
                ztmodelDataModelVo.setIsMark("1");
                List<CfgExtend> cfgExtends = extCfgExtendMapper.getExtendBydbkey(cb.getDbKey());
                if (cfgExtends != null) {
                    List<ZTreeDataModelVo> childztree = new ArrayList<>();
                    for (CfgExtend ced : cfgExtends) {
                        ZTreeDataModelVo childzd = new ZTreeDataModelVo();
                        childzd.setId(ced.getGuid());
                        childzd.setName(ced.getServicename());
                        childzd.setHint("单表服务:" + ced.getGuid());
                        childzd.setIsMark("2");
                        childztree.add(childzd);
                    }
                    ztmodelDataModelVo.setChildren(childztree);
                }
                ztreeList.add(ztmodelDataModelVo);
            }
        }

        return ztreeList;
    }

    @Override
    public Integer insertCfgExtendandSysprjs(CfgExtendExt cfgExtendExt) {
        Integer result = 0;
        //添加单表服务配置
        CfgExtend cfgExtend = new CfgExtend();
        cfgExtend.setGuid(cfgExtendExt.getGuid());
        cfgExtend.setDbKey(cfgExtendExt.getDbKey());
        cfgExtend.setDtConfigId(cfgExtendExt.getDtConfigId());
        cfgExtend.setServicename(cfgExtendExt.getServicename());
        cfgExtend.setResponseMode(cfgExtendExt.getResponseMode());
        if (cfgExtendExt.getIsPagination() != null && !cfgExtendExt.getIsPagination().equals("")) {
            cfgExtend.setIspagination(Integer.valueOf(cfgExtendExt.getIsPagination()));
        }
        cfgExtend.setRemark(cfgExtendExt.getRemark());
        if (cfgExtendExt.getSysCatch() != null && !cfgExtendExt.getSysCatch().equals("")) {
            cfgExtend.setSyscatch(Integer.valueOf(cfgExtendExt.getSysCatch()));
        }
        if (cfgExtendExt.getCatchtime() != null && !cfgExtendExt.getCatchtime().equals("")) {
            cfgExtend.setCatchtime(Integer.valueOf(cfgExtendExt.getCatchtime()));
        }
        Date datenow = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cfgExtend.setCreatetime(dateFormat.format(datenow));
        cfgExtend.setTimeDelay(cfgExtendExt.getTimeDelay());
        result += cfgExtendMapper.insertSelective(cfgExtend);

        //添加工程服务
        SysPrjservice sysPrjservice = new SysPrjservice();
        sysPrjservice.setGuid(UUID.randomUUID().toString());
        sysPrjservice.setPcode(cfgExtendExt.getPcode());
        sysPrjservice.setServicetype(cfgExtendExt.getServicetype());
        sysPrjservice.setServicecode(cfgExtendExt.getGuid());
        sysPrjservice.setCreatedate(datenow);
        sysPrjservice.setSysDataLabelId(cfgExtendExt.getSysDataLabelId());
        sysPrjservice.setCacheDur(cfgExtendExt.getCacheDur());
        sysPrjservice.setApiPath(cfgExtendExt.getApiPath());
        sysPrjservice.setResCvsJs(cfgExtendExt.getResCvsJs());
        sysPrjservice.setServicename(cfgExtendExt.getServicename());
        sysPrjservice.setTableCodes(cfgExtendExt.getTableCodes());
        result += sysPrjserviceMapper.insertSelective(sysPrjservice);

        redisCacheComponent.freshExtendRedisCache();
        return result;
    }

    @Override
    public String getdbtypeByserviceId(String serviceId) {
        String result = "";
        CfgExtend cfgExtend = cfgExtendMapper.selectByPrimaryKey(serviceId);
        if (cfgExtend != null) {
            CfgDb cfgDb = cfgDbMapper.selectByPrimaryKey(cfgExtend.getDbKey());
            if (cfgDb != null) {
                result = cfgDb.getDbVersion();
            }
        }
        return result;
    }


}
